/*
 * HessingerBGView.java
 */

package hessingerbg.gui;

import com.l2fprod.gui.plaf.skin.Skin;
import com.l2fprod.gui.plaf.skin.SkinLookAndFeel;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.MalformedURLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.jdesktop.application.Action;
import org.jdesktop.application.ResourceMap;
import org.jdesktop.application.SingleFrameApplication;
import org.jdesktop.application.FrameView;
import org.jdesktop.application.TaskMonitor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Timer;
import javax.swing.Icon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JSpinner;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import javax.swing.SpinnerModel;
import javax.swing.SpinnerNumberModel;
import javax.swing.UIManager;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.text.DefaultStyledDocument;
import javax.swing.text.StyledDocument;
import net.java.dev.designgridlayout.DesignGridLayout;

/**
 * The application's main frame.
 */
public class HessingerBGView extends FrameView {

    public HessingerBGView(SingleFrameApplication app) {
        super(app);

            try {
                Skin theSkinToUse = SkinLookAndFeel.loadThemePack("themepack.zip");
                SkinLookAndFeel.setSkin(theSkinToUse);
                UIManager.setLookAndFeel(new SkinLookAndFeel());

            } catch (Exception ex) {
                Logger.getLogger(HessingerBGView.class.getName()).log(Level.SEVERE, null, ex);
            }



        jMenuItem1ActionPerformed(null);
        initComponents();
        createAndAddPanels();

        
        try
        {
            thread = new hessingerbg.codebehind.routine(outlog);
            new Thread(thread).start();
        }
        catch(Exception e)
        {
           JOptionPane.showMessageDialog(list, "An Error Occured, Routine Stop! Restart Application ");
           e.printStackTrace();
        }

        // status bar initialization - message timeout, idle icon and busy animation, etc
        ResourceMap resourceMap = getResourceMap();
        int messageTimeout = resourceMap.getInteger("StatusBar.messageTimeout");
        messageTimer = new Timer(messageTimeout, new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                statusMessageLabel.setText("");
            }
        });
        messageTimer.setRepeats(false);
        int busyAnimationRate = resourceMap.getInteger("StatusBar.busyAnimationRate");
        for (int i = 0; i < busyIcons.length; i++) {
            busyIcons[i] = resourceMap.getIcon("StatusBar.busyIcons[" + i + "]");
        }
        busyIconTimer = new Timer(busyAnimationRate, new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                busyIconIndex = (busyIconIndex + 1) % busyIcons.length;
                statusAnimationLabel.setIcon(busyIcons[busyIconIndex]);
            }
        });
        idleIcon = resourceMap.getIcon("StatusBar.idleIcon");
        statusAnimationLabel.setIcon(idleIcon);
        progressBar.setVisible(false);

        // connecting action tasks to status bar via TaskMonitor
        TaskMonitor taskMonitor = new TaskMonitor(getApplication().getContext());
        taskMonitor.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                String propertyName = evt.getPropertyName();
                if ("started".equals(propertyName)) {
                    if (!busyIconTimer.isRunning()) {
                        statusAnimationLabel.setIcon(busyIcons[0]);
                        busyIconIndex = 0;
                        busyIconTimer.start();
                    }
                    progressBar.setVisible(true);
                    progressBar.setIndeterminate(true);
                } else if ("done".equals(propertyName)) {
                    busyIconTimer.stop();
                    statusAnimationLabel.setIcon(idleIcon);
                    progressBar.setVisible(false);
                    progressBar.setValue(0);
                } else if ("message".equals(propertyName)) {
                    String text = (String)(evt.getNewValue());
                    statusMessageLabel.setText((text == null) ? "" : text);
                    messageTimer.restart();
                } else if ("progress".equals(propertyName)) {
                    int value = (Integer)(evt.getNewValue());
                    progressBar.setVisible(true);
                    progressBar.setIndeterminate(false);
                    progressBar.setValue(value);
                }
            }
        });
    }

    @Action
    public void showAboutBox() {
        if (aboutBox == null) {
            JFrame mainFrame = HessingerBGApp.getApplication().getMainFrame();
            aboutBox = new HessingerBGAboutBox(mainFrame);
            aboutBox.setLocationRelativeTo(mainFrame);
        }
        HessingerBGApp.getApplication().show(aboutBox);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        mainPanel = new javax.swing.JPanel();
        jTabbedPane1 = new javax.swing.JTabbedPane();
        jToggleButton1 = new javax.swing.JToggleButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        outlog = new javax.swing.JTextPane();
        menuBar = new javax.swing.JMenuBar();
        javax.swing.JMenu fileMenu = new javax.swing.JMenu();
        javax.swing.JMenuItem exitMenuItem = new javax.swing.JMenuItem();
        javax.swing.JMenu helpMenu = new javax.swing.JMenu();
        jMenuItem1 = new javax.swing.JMenuItem();
        javax.swing.JMenuItem aboutMenuItem = new javax.swing.JMenuItem();
        statusPanel = new javax.swing.JPanel();
        javax.swing.JSeparator statusPanelSeparator = new javax.swing.JSeparator();
        statusMessageLabel = new javax.swing.JLabel();
        statusAnimationLabel = new javax.swing.JLabel();
        progressBar = new javax.swing.JProgressBar();

        mainPanel.setName("mainPanel"); // NOI18N

        jTabbedPane1.setName("jTabbedPane1"); // NOI18N

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(hessingerbg.gui.HessingerBGApp.class).getContext().getResourceMap(HessingerBGView.class);
        jToggleButton1.setText(resourceMap.getString("jToggleButton1.text")); // NOI18N
        jToggleButton1.setName("jToggleButton1"); // NOI18N
        jToggleButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jToggleButton1ActionPerformed(evt);
            }
        });

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        outlog.setName("outlog"); // NOI18N
        jScrollPane1.setViewportView(outlog);

        javax.swing.GroupLayout mainPanelLayout = new javax.swing.GroupLayout(mainPanel);
        mainPanel.setLayout(mainPanelLayout);
        mainPanelLayout.setHorizontalGroup(
            mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(mainPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 430, Short.MAX_VALUE)
                    .addComponent(jTabbedPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 430, Short.MAX_VALUE)
                    .addComponent(jToggleButton1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 63, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );
        mainPanelLayout.setVerticalGroup(
            mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(mainPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jTabbedPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 203, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 194, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jToggleButton1)
                .addContainerGap())
        );

        menuBar.setName("menuBar"); // NOI18N

        fileMenu.setText(resourceMap.getString("fileMenu.text")); // NOI18N
        fileMenu.setName("fileMenu"); // NOI18N

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(hessingerbg.gui.HessingerBGApp.class).getContext().getActionMap(HessingerBGView.class, this);
        exitMenuItem.setAction(actionMap.get("quit")); // NOI18N
        exitMenuItem.setName("exitMenuItem"); // NOI18N
        fileMenu.add(exitMenuItem);

        menuBar.add(fileMenu);

        helpMenu.setText(resourceMap.getString("helpMenu.text")); // NOI18N
        helpMenu.setName("helpMenu"); // NOI18N

        jMenuItem1.setText(resourceMap.getString("jMenuItem1.text")); // NOI18N
        jMenuItem1.setName("jMenuItem1"); // NOI18N
        jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem1ActionPerformed(evt);
            }
        });
        helpMenu.add(jMenuItem1);

        aboutMenuItem.setAction(actionMap.get("showAboutBox")); // NOI18N
        aboutMenuItem.setName("aboutMenuItem"); // NOI18N
        helpMenu.add(aboutMenuItem);

        menuBar.add(helpMenu);

        statusPanel.setName("statusPanel"); // NOI18N

        statusPanelSeparator.setName("statusPanelSeparator"); // NOI18N

        statusMessageLabel.setName("statusMessageLabel"); // NOI18N

        statusAnimationLabel.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        statusAnimationLabel.setName("statusAnimationLabel"); // NOI18N

        progressBar.setName("progressBar"); // NOI18N

        javax.swing.GroupLayout statusPanelLayout = new javax.swing.GroupLayout(statusPanel);
        statusPanel.setLayout(statusPanelLayout);
        statusPanelLayout.setHorizontalGroup(
            statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(statusPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(statusMessageLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 276, Short.MAX_VALUE)
                .addComponent(progressBar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(statusAnimationLabel)
                .addContainerGap())
            .addComponent(statusPanelSeparator, javax.swing.GroupLayout.DEFAULT_SIZE, 450, Short.MAX_VALUE)
        );
        statusPanelLayout.setVerticalGroup(
            statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, statusPanelLayout.createSequentialGroup()
                .addGap(2, 2, 2)
                .addComponent(statusPanelSeparator, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(statusMessageLabel)
                    .addComponent(statusAnimationLabel)
                    .addComponent(progressBar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(3, 3, 3))
        );

        setComponent(mainPanel);
        setMenuBar(menuBar);
        setStatusBar(statusPanel);
    }// </editor-fold>//GEN-END:initComponents

    private void jToggleButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jToggleButton1ActionPerformed

       if(!jToggleButton1.isSelected())
       {
            thread.Pause();
            for(int x = 0; x < panels.length; x++)
                panels[x].reverseLockAllComponents();
       }
       else
       {
            for(int x = 0; x < panels.length; x++)
                panels[x].reverseLockAllComponents();
            this.saveMovement();

            thread.Resume();
       }
    }//GEN-LAST:event_jToggleButton1ActionPerformed

    private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem1ActionPerformed
        try {
            JFrame patchnotes = new JFrame();
            DesignGridLayout x = new DesignGridLayout(patchnotes.getContentPane());
            JTextPane notes = new JTextPane();
            JScrollPane sc = new JScrollPane(notes);
            notes.setText(Version.getPatchnotes());
            x.row().center().add(sc).fill();
            patchnotes.setSize(400, 500);
            patchnotes.setVisible(true);
            patchnotes.setLocationRelativeTo(HessingerBGApp.getApplication().getMainFrame());
            patchnotes.setTitle("Patch Notes");
        } catch (FileNotFoundException ex) {
            Logger.getLogger(HessingerBGView.class.getName()).log(Level.SEVERE, null, ex);
        } catch (MalformedURLException ex) {
            Logger.getLogger(HessingerBGView.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(HessingerBGView.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_jMenuItem1ActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JToggleButton jToggleButton1;
    private javax.swing.JPanel mainPanel;
    private javax.swing.JMenuBar menuBar;
    private javax.swing.JTextPane outlog;
    private javax.swing.JProgressBar progressBar;
    private javax.swing.JLabel statusAnimationLabel;
    private javax.swing.JLabel statusMessageLabel;
    private javax.swing.JPanel statusPanel;
    // End of variables declaration//GEN-END:variables

    private final Timer messageTimer;
    private final Timer busyIconTimer;
    private final Icon idleIcon;
    private final Icon[] busyIcons = new Icon[15];
    private int busyIconIndex = 0;

    private JDialog aboutBox;

    private Panel[] panels = new Panel[3];
    hessingerbg.codebehind.spellmanager manager;
    hessingerbg.codebehind.movementmanager mmanager;
    JList list;
    hessingerbg.codebehind.routine thread; //= new hessBG.routine(outlog);

    // General Screen
    JComboBox togglefriendly = new JComboBox(KeyVector.getKeys());
    JComboBox toggleenemy = new JComboBox(KeyVector.getKeys());

    // General Screen with Movement
    JComboBox bg1 = new JComboBox(KeyVector.getKeys());
    JComboBox bg2 = new JComboBox(KeyVector.getKeys());
    JComboBox qa = new JComboBox(KeyVector.getKeys());
    JComboBox pvp = new JComboBox(KeyVector.getKeys());


    // Abilities Screen
    JTextField spellname = new JTextField();
    JComboBox spellbind = new JComboBox(KeyVector.getKeys());
    JComboBox spellbindmod = new JComboBox(KeyVector.getModifiers());
    JCheckBox forceattempt = new JCheckBox();
    JCheckBox active = new JCheckBox();
    JCheckBox casting = new JCheckBox();
    JButton newspell = new JButton("New");
    SpinnerModel cdmodel = new SpinnerNumberModel(0, 0, 1000, 1);
    JButton updatespell = new JButton("Update");
    JSpinner cooldown = new JSpinner(cdmodel);
    SpinnerModel castingmodel = new SpinnerNumberModel(0, 0, 10, .1);
    JSpinner castingtime = new JSpinner(castingmodel);
    SpinnerModel spellratingmodel = new SpinnerNumberModel(0, 0, 1000, 1);
    JSpinner spellrating = new JSpinner(spellratingmodel);
    JCheckBox friendlyTarget = new JCheckBox();
    JCheckBox enemyTarget = new JCheckBox();


    // Movement Screen
    JCheckBox enablemovement = new JCheckBox();
    JComboBox moveforward = new JComboBox(KeyVector.getKeys());
    JComboBox turnleft = new JComboBox(KeyVector.getKeys());
    JComboBox turnright = new JComboBox(KeyVector.getKeys());
    JComboBox moveright = new JComboBox(KeyVector.getKeys());
    JComboBox moveleft = new JComboBox(KeyVector.getKeys());
    JCheckBox enablefollow = new JCheckBox();
    JComboBox follow = new JComboBox(KeyVector.getKeys());

    private void saveMovement()
    {
        hessingerbg.codebehind.movement move;
        if(mmanager.size() != 0)
        {
            move = mmanager.get(0);
        }
        else
        {
            move = new hessingerbg.codebehind.movement();
            mmanager.add(move);
        }

        for(hessingerbg.codebehind.spell x : manager)
        {
            x.setTogglefriendly(KeyVector.getKeyValue((String)togglefriendly.getSelectedItem()));
            x.setTogglehostile(KeyVector.getKeyValue((String)toggleenemy.getSelectedItem()));
        }


        move.setFollow(KeyVector.getKeyValue((String)follow.getSelectedItem()));
        move.setFollowing(enablefollow.isSelected());
        move.setMoveforward(KeyVector.getKeyValue((String)moveforward.getSelectedItem()));
        move.setMoveleft(KeyVector.getKeyValue((String)moveleft.getSelectedItem()));
        move.setMovement(enablemovement.isSelected());
        move.setMoveright(KeyVector.getKeyValue((String)moveright.getSelectedItem()));
        move.setTurnleft(KeyVector.getKeyValue((String)turnleft.getSelectedItem()));
        move.setTurnright(KeyVector.getKeyValue((String)turnright.getSelectedItem()));

        // BG
        move.setBg1(KeyVector.getKeyValue((String)bg1.getSelectedItem()));
        move.setBg2(KeyVector.getKeyValue((String)bg2.getSelectedItem()));
        move.setAcceptqueue(KeyVector.getKeyValue((String)qa.getSelectedItem()));
        move.setPvpwindow(KeyVector.getKeyValue((String)pvp.getSelectedItem()));


        try {
            mmanager.writeFile();
            manager.writeFile();
        } catch (Exception ex) {
            System.out.println("Unable to write file");
        }
    }


    private void createAndAddPanels() {

        try {
            manager = hessingerbg.codebehind.spellmanager.readFile();
        } catch (Exception ex) {
            manager = new hessingerbg.codebehind.spellmanager();
        }


        try {
            mmanager = hessingerbg.codebehind.movementmanager.readFile();
            hessingerbg.codebehind.movement move = mmanager.get(0);
            moveforward.setSelectedItem(String.valueOf((char)move.getMoveforward()));
            moveleft.setSelectedItem(String.valueOf((char)move.getMoveleft()));
            moveright.setSelectedItem(String.valueOf((char)move.getMoveright()));
            turnleft.setSelectedItem(String.valueOf((char)move.getTurnleft()));
            turnright.setSelectedItem(String.valueOf((char)move.getTurnright()));
            follow.setSelectedItem(String.valueOf((char)move.getFollow()));

            // BG
            bg1.setSelectedItem(String.valueOf((char)move.getBg1()));
            bg2.setSelectedItem(String.valueOf((char)move.getBg2()));
            qa.setSelectedItem(String.valueOf((char)move.getAcceptqueue()));
            pvp.setSelectedItem(String.valueOf((char)move.getPvpwindow()));

            enablefollow.setSelected(move.isFollowing());
            enablemovement.setSelected(move.isMovement());

            toggleenemy.setSelectedItem(String.valueOf((char)manager.firstElement().getTogglehostile()));
            togglefriendly.setSelectedItem(String.valueOf((char)manager.firstElement().getTogglefriendly()));
        } catch (Exception ex) {
            mmanager = new hessingerbg.codebehind.movementmanager();
        }



        list = new JList(manager);


        panels[0] = new Panel("General");
        panels[1] = new Panel("Abilities");
        panels[2] = new Panel("Movement");

        for(int i = 0; i < panels.length; i++ )
        {
            jTabbedPane1.addTab(panels[i].getName(), panels[i]);
        }

        /*
         *
         * GENERAL
         *
         */

        StyledDocument x = new DefaultStyledDocument();
        JTextPane notes = new JTextPane();

        notes.setText("\n The following macros need to be binded" +
                "\n Macro 1: BG1 and BG2" +
                "\n Replace the SelectBG = #; with the # coresponding to your PVP window's battleground list" +
                "\n the first bg starts at 1 rather than 0\n" +
                "\n/script PVPBattlegroundFrame.selectedBG = 6;\n/script JoinBattlefield(0)\n\n" +
                "" +
                "\n Macros 2: Accept Queue \n" +
                "\n/script AcceptBattlefieldPort(1,1);\n/script AcceptBattlefieldPort(2,1);\n/script AcceptBattlefieldPort(3,1);\n/script AcceptBattlefieldPort(4,1);\n/script AcceptBattlefieldPort(5,1);\n/script AcceptBattlefieldPort(6,1);");

        JScrollPane JSPane = new JScrollPane(notes);
        JSPane.setWheelScrollingEnabled(true);

        panels[0].addComponent(0, toggleenemy, togglefriendly, "Toggle Hostile", "Toggle Friendly", true);
        panels[0].addComponent(0, bg1, bg2, "BG1", "BG2", true);
        panels[0].addComponent(0, qa , pvp , "Accept Queue" , "PVP Window" , true);
        panels[0].addComponent(1, JSPane , "Notes:");


        /*
         *
         * MOVEMENT
         *
         */

        panels[2].addComponent(0, enablemovement,  enablefollow,"Enable Movement",  "Enable Follow", true);
        panels[2].addComponent(0, moveforward, follow, "Move Forward", "Follow", true);
        panels[2].addComponent(0, turnleft, turnright, "Turn Left", "Turn Right", true);
        panels[2].addComponent(0, moveleft, moveright, "Move Left", "Move Right", true);
        //panels[2].addComponent(1, new JTextField(), "Routine Customization (Not Implemented)");

        /*
         *
         * ABILITIES PANEL
         *
         */

        panels[1].addComponent(0, new JScrollPane(list), "List");
        panels[1].addComponent(1, spellname, cooldown, "Name", "CD (Secs)", true);
        panels[1].addComponent(1, spellbindmod, spellbind, "Modifier", "Bind", true);
        panels[1].addComponent(1, active, spellrating, "Active", "Priority 0>1", true);
        panels[1].addComponent(1, casting,  castingtime, "Casting", "Time", true);
        panels[1].addComponent(1, this.friendlyTarget, this.enemyTarget, "Friendly", "Hostile", true);
        panels[1].addComponent(1, newspell, updatespell, "New", "Update", false);


        if(manager.size() == 0)
        {
            newspell.setEnabled(false);
            panels[1].reverseLockAllComponents();
        }
        newspell.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
            	manager.add(new hessingerbg.codebehind.spell());
                list.setListData(manager);
                forceattempt.setEnabled(false);
                forceattempt.setToolTipText("Disabled as of Build 1.00");
            }
        });

        list.addListSelectionListener(new ListSelectionListener() {

            public void valueChanged(ListSelectionEvent e) {
                if(!e.getValueIsAdjusting() && !list.isSelectionEmpty())
                {
                    hessingerbg.codebehind.spell spell = (hessingerbg.codebehind.spell)list.getSelectedValue();
                    spellname.setText(spell.getName());
                    spellbind.setSelectedItem(String.valueOf((char)spell.getKeybind()));
                    spellbindmod.setSelectedItem(KeyVector.getModifierSelection(spell.getKeymod()));
                    forceattempt.setSelected(spell.isForce());
                    active.setSelected(spell.isIsactive());
                    cdmodel.setValue(spell.getCooldown());
                    castingmodel.setValue(spell.getCastingtime());
                    spellrating.setValue(spell.getUsage());
                    casting.setSelected(spell.isRequirescasting());
                    friendlyTarget.setSelected(spell.isFriendly());
                    enemyTarget.setSelected(spell.isHostile());


                    updatespell.setEnabled(true);
                    spellname.setEnabled(true);
                    spellbind.setEnabled(true);
                    spellbindmod.setEnabled(true);
                    forceattempt.setEnabled(true);
                    active.setEnabled(true);
                    cooldown.setEnabled(true);
                    casting.setEnabled(true);
                    castingtime.setEnabled(true);
                    spellrating.setEnabled(true);
                    friendlyTarget.setEnabled(true);
                    enemyTarget.setEnabled(true);

                }
            }

        });
        updatespell.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                if(spellname.getText().length() != 0)
                {
                    if(!list.isSelectionEmpty())
                    {
                        hessingerbg.codebehind.spell spell = (hessingerbg.codebehind.spell)list.getSelectedValue();
                        spell.setName(spellname.getText());
                        spell.setKeybind(KeyVector.getKeyValue((String)spellbind.getSelectedItem()));
                        spell.setKeymod(KeyVector.getModifierValue((String)spellbindmod.getSelectedItem()));
                        spell.setForce(forceattempt.isSelected());
                        spell.setIsactive(active.isSelected());
                        spell.setCooldown(((Integer)cdmodel.getValue()).intValue());
                        spell.setCastingtime(((Double)castingmodel.getValue()).doubleValue());
                        spell.setUsage((Integer)spellratingmodel.getValue());
                        spell.setRequirescasting(casting.isSelected());
                        spell.setFriendly(friendlyTarget.isSelected());
                        spell.setHostile(enemyTarget.isSelected());

                        try {
                            manager.writeFile();
                        } catch (Exception ex) {
                            System.err.println(ex.getMessage());
                        }
                        list.setListData(manager);

                    }
                    else
                    {
                        JOptionPane.showMessageDialog(list, "You must select an abilitiy to update");
                    }
                }
                else
                {
                    JOptionPane.showMessageDialog(list, "You must enter a name");
                }
            }
        });

    }
}
